package com.wzp.yyptproject.service;

import com.wzp.yyptproject.entity.*;
import com.wzp.yyptproject.repository.DoctorRepository;
import com.wzp.yyptproject.repository.KeShiRepository;
import com.wzp.yyptproject.repository.UserRepository;
import com.wzp.yyptproject.repository.YuYueRepository;
import com.wzp.yyptproject.until.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class YuYueServiceimp implements YuYueService {
    @Autowired
    private YuYueRepository yuYueRepository;
     @Autowired
    private DoctorRepository doctorRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private KeShiRepository keShiRepository;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<YuYue> findAndByUid(String uname ,String uidcard ) {
        User user = userRepository.findAndByUnameAndUidcard(uname,uidcard);
        Integer uid = user.getUUid();
        List<YuYue> yuYueList = yuYueRepository.findAndByUid(uid);
            return yuYueList;
    }



    @Override
    public List<YuYue2> findAllyuyue(List<YuYue> list ) {
        List<YuYue2> list1=new ArrayList<>();
        for (YuYue yuYue:list){
            YuYue2  yuYue2=new YuYue2();
            yuYue2.setOid(yuYue.getOid());
            yuYue2.setOstatus(yuYue.getOstatus());
            yuYue2.setOtimes(yuYue.getOtimes());
            yuYue2.setOdates(yuYue.getOdates());
            yuYue2.setUid(yuYue.getUid());
            User user = userRepository.findAndByUUid(yuYue.getUid());
            yuYue2.setUname(user.getUname());

            yuYue2.setDid(yuYue.getDid());
            Doctor doctor = doctorRepository.findAndByDDid(yuYue.getDid());
            yuYue2.setDname(doctor.getDDname());
            yuYue2.setKid(yuYue.getKid());
            KeShi keShi = keShiRepository.findAllByKid(yuYue.getKid());
            yuYue2.setKname(keShi.getKname());

            list1.add(yuYue2);
        }
        return list1;
    }

    @Override
    public String addYuYue(String uname, String uidcard, Integer did, String odates, String otimes){
        YuYue yuYue=new YuYue();
        User user = userRepository.findAndByUnameAndUidcard(uname,uidcard);
        yuYue.setUid(user.getUUid());
        Doctor doctor = doctorRepository.findAndByDDid(did);
        yuYue.setDid(did);
        yuYue.setKid(doctor.getKKid());
        yuYue.setOdates(odates);
        yuYue.setOtimes(otimes);
        yuYue.setBid(doctor.getBBid());
        yuYue.setOstatus(1);
        System.out.println("人数1***"+yuYueRepository.findAllByDidAndOtimesAndOdates(did,otimes,odates).size());
        if (yuYueRepository.findAllByDidAndOtimesAndOdates(did,otimes,odates).size()==2){
            System.out.println("人数2***"+yuYueRepository.findAllByDidAndOtimesAndOdates(did,otimes,odates).size());
            return "failed1";
        }
        if(yuYueRepository.findAllByUidAndOtimesAndOdates(yuYue.getUid(),otimes,odates).size()!=0){
            System.out.println(yuYueRepository.findAllByUidAndOtimesAndOdates(yuYue.getUid(),otimes,odates).size());
            return "failed2";
        }

        if (yuYueRepository.findAllByDidAndOtimesAndOdates(did,otimes,odates).size()<2){
            System.out.println("22222212");
            yuYueRepository.save(yuYue);
            return "success";
        }
       return "failed";
    }
//    @Override
//    public boolean deleteYuYue(String uname, String uidcard){
//
//        User user = userRepository.findAndByUnameAndUidcard(uname,uidcard);
//        System.out.println(user.getUUid());
//        yuYueRepository.deleteById(user.getUUid());
//        return true;
//    }
@Override
public boolean deleteYuYue(Integer oid){


    yuYueRepository.deleteById(oid);
    return true;
}

    public String getChickCode() {
        final String source = "1908253674";
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            str.append(source.charAt(random.nextInt(source.length() - 1)));
        }
        String data = str.toString();
//      redisTemplate.opsForValue().set("logincode",data);
        redisUtils.set("yuyuema", data);
        redisUtils.expire("checkCode", 60*3600*5);
        return data;
    }
}
